package action;

import java.util.Date;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import pojo.Appuser;
import service.UserService;
import service.impl.UserServiceImpl;

public class RegisterAction extends ActionSupport {

	private Appuser appuser = new Appuser();
	private UserService userService =new UserServiceImpl();
	private String username;
	private String pwd;
	


	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	Map<String, Object> session = ActionContext.getContext().getSession();
	
	//用户注册
	public String register(){
	
		appuser.setUsername(username);  
		appuser.setPwd(pwd); 
		appuser.setCreatetime(new Date());
	    if(userService.register(appuser)){ 
	            session.put("res_appuser", appuser);
	            return "register_success";
	            
	        }else{  
	        	return "register_failure";
	        }     
	}
	//校验
	public void validate(){
		String regex = "1[3|4|5|7|8|][0-9]{9}";
		//String regex2 = "^[\u4e00-\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}$";
		if("".equals(username) || ! username.matches(regex)){
			this.addFieldError("userError", "请输入有效的手机号码");
		}
		if(pwd.length()<6 || pwd.length()>12){
			this.addFieldError("pwdError", "密码为3到8位的非汉字符");
		}
		/*if("".equals(username)){
			this.addFieldError("userError", "用户名不能为空");
		}
		if("".equals(pwd)){
			this.addFieldError("pwdError", "密码为3到8位的非汉字符");
		}*/
	}
	

	public Appuser getAppuser() {
		return appuser;
	}

	public void setAppuser(Appuser appuser) {
		this.appuser = appuser;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	

	

}
